package generic;

import javax.lang.model.type.ArrayType;
import java.lang.reflect.Array;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;

public class ArrayGenericTestMain {
    public <T> T[] getArray(Class<T> type, int size) {
        return (T[]) Array.newInstance(type, size);
    }

    public <T> void judgeType(T array) {
        String typeName = array.getClass().getGenericSuperclass().getTypeName();
        Type arrayType = array.getClass().getGenericSuperclass();

        System.out.println("arrayType class = " + arrayType.getClass());


        if (arrayType instanceof ParameterizedType) {
            System.out.println("arrayType is ParameterizedType");
        } else {
            System.out.println("arrayType is not ParameterizedType");
        }
        Type actualType = ((ParameterizedType) arrayType).getActualTypeArguments()[0];

        System.out.println("typeName = "+ typeName);
        System.out.println("actualType = "+ actualType);
    }

    public static void test() {

    }

    public static void main(String[] args) {
        Object[] objects;
        String[] strs = new String[10];
        objects = strs;
//        objects[0] = Integer.valueOf(0);
        ArrayList<String> arrayList = new ArrayList<String>();
        arrayList.add("666");

        System.out.println("strs' type is" + strs.getClass());
        System.out.println("arrayList' type is" + arrayList.getClass());

//        ArrayList<Object> objectList = arrayList;

        ArrayList arrayList1 = new ArrayList();
        ArrayList arrayList2 = new ArrayList<String>();

        Integer[] intArray = new ArrayGenericTestMain().getArray(Integer.class, 10);
        for (int i = 0; i < intArray.length; i++) {
            intArray[i] = i;
        }

        for (int i = 0; i < intArray.length; i++) {
            System.out.println("intArray[" + i + "] = " + intArray[i]);
        }

        new ArrayGenericTestMain().judgeType(arrayList);
    }
}